<template>
  <!-- 添加或修改定时任务对话框 -->
  <el-dialog :title="title" :visible.sync="visible" width="700px" append-to-body :close-on-click-modal="false" @closed="reset">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="form.jobName" placeholder="请输入任务名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务分组" prop="jobGroup">
            <el-select v-model="form.jobGroup" placeholder="请选择">
              <el-option
                v-for="dict in jobGroupOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调用方法" prop="invokeTarget">
            <el-input v-model="form.invokeTarget" placeholder="请输入调用目标字符串"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="cron表达式" prop="cronExpression">
            <el-input v-model="form.cronExpression" placeholder="请输入cron执行表达式"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="submitting" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {addJob, getJob, updateJob} from "@/api/monitor/job";

export default {
  name: "edit",
  props: {
    // 任务组名字典
    jobGroupOptions: {
      type: Array,
      default: []
    },
    statusOptions: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      id: undefined,
      title: "新增编辑",
      visible: false,
      submitting: false,
      form: {
        jobName: undefined,
        jobGroup: undefined,
        invokeTarget: undefined,
        cronExpression: undefined,
        misfirePolicy: 1,
        concurrent: 1,
        status: "0"
      },
      // 表单校验
      rules: {
        jobName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        invokeTarget: [
          { required: true, message: "调用目标字符串不能为空", trigger: "blur" }
        ],
        cronExpression: [
          { required: true, message: "cron执行表达式不能为空", trigger: "blur" }
        ]
      }
    }
  },
  methods: {
    init(id, title) {
      this.visible = true;
      this.id = id;
      this.title = title;
      if (id) {
        this.initData();
      }
    },
    initData() {
      getJob(this.id).then(response => {
        this.form = response;
      });
    },
    close() {
      this.visible = false;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitting = true;
          if (this.id) {
            updateJob(this.id, this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("修改成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          } else {
            addJob(this.form).then(response => {
              this.submitting = false;

              this.msgSuccess("新增成功");
              this.close();
              this.$emit('success');
            }).catch(() => {
              this.submitting = false;
            });
          }
        }
      });
    },
    // 表单重置
    reset() {
      this.id = undefined;
      this.form = {
        jobName: undefined,
        jobGroup: undefined,
        invokeTarget: undefined,
        cronExpression: undefined,
        misfirePolicy: 1,
        concurrent: 1,
        status: "0"
      };
      this.resetForm("form");
    }
  }
}
</script>

<style scoped>

</style>
